package cn.itlaobing.web.controller;

import cn.itlaobing.entity.EmployeeModel;
import cn.itlaobing.service.EmployService;
import cn.itlaobing.util.Gender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Date;

/**
 * Created by Administrator on 2017-12-02.
 */
@Controller
@RequestMapping("/employ")
public class EmployController {
    @Autowired
    private EmployService employService;

    @RequestMapping("/queryall")
    public String findAll(Model model)
    {
        model.addAttribute("EMPLOYEELIST",employService.queryAll());
        return "index";
    }
    @GetMapping("/{id}/update")
    public String updateBefore(@PathVariable Integer id, Model model)
    {
        model.addAttribute("Employ",employService.findByid(id));
        return "/update";
    }
    @PostMapping("/{id}/update")
    public String update(@PathVariable Integer id,EmployeeModel employeeModel,RedirectAttributes redirectAttributes)
    {
        employeeModel.setId(id);
        //employeeModel.setBirthday(new Date());
        employService.updateEmployee(employeeModel);
        redirectAttributes.addFlashAttribute("msg","修改成功");
        return "redirect:/employ/queryall";
    }

    //同步解决方案
   /* @GetMapping("/{id}/delete")
    public String delete(@PathVariable Integer id)
    {
        employService.deleteEmployByid(id);
        return "redirect:/employ/queryall";
    }*/

   //异步请求方式
    @GetMapping("/{id}/delete")
    @ResponseBody
    public String delete(@PathVariable Integer id)
    {
        employService.deleteEmployByid(id);
        return "ok";
    }
    @GetMapping("/add")
    public String add(Model model)
    {
        model.addAttribute("Employ",new EmployeeModel());
        model.addAttribute("Gender", Gender.values());
        return "add";
    }
    @PostMapping("/add")
    public String add(EmployeeModel employeeModel, RedirectAttributes redirectAttributes)
    {

        employService.addEmploy(employeeModel);
        redirectAttributes.addFlashAttribute("msg","增加成功");
        return "redirect:/employ/queryall";
    }

}
